﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Store.WP7.Data.WebStore;
using System.Collections.ObjectModel;
using Store.WP7.Data;
using System.Windows.Media.Imaging;
using System.Globalization;

namespace Store.WP7.ViewModels
{
    public class OrderViewModel : ViewModelBase
    {
        private readonly Order _innerOrder;

        public Order InnerOrder
        {
            get { return _innerOrder; }
        }

        //public OrderViewModel() { }
        public OrderViewModel(Order order)
        {
            _innerOrder = order;
            InitializeViewModel();
        }

        private void InitializeViewModel()
        {
            TotalPrice = string.Format(new CultureInfo("fr-fr"), "{0:C}", _innerOrder.TotalPrice);
            OrderDate = string.Format("{0:m} {0:yyyy}", _innerOrder.CreationDate);
            ModificationDate = string.Format("{0:m} {0:yyyy}", _innerOrder.ModificationDate);
            State = OrderStateHelper.GetOrderStateLabel(_innerOrder.State);
            IsShipped = _innerOrder.State == 3;

            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                StatePicture = new BitmapImage(OrderStateHelper.GetOrderStatePictureUri(_innerOrder.State));
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(() => StatePicture = new BitmapImage(OrderStateHelper.GetOrderStatePictureUri(_innerOrder.State)));
            }

            foreach (var product in _innerOrder.Products)
            {
                Products.Add(new ProductViewModel(product));
            }
        }

        private ImageSource _statePicture;
        public ImageSource StatePicture
        {
            get { return _statePicture; }
            set 
            { 
                _statePicture = value;
                RaisePropertyChanged(() => StatePicture);
            }
        }

        public string ShippingLabel { get { return _innerOrder.ShipAddress.Label; } }

        public string ShippingFirstName { get { return _innerOrder.ShipAddress.FirstName; } }

        public string ShippingLastName { get { return _innerOrder.ShipAddress.LastName; } }

        public string ShippingStreet { get { return _innerOrder.ShipAddress.Street; } }

        public string ShippingZipCode { get { return _innerOrder.ShipAddress.ZipCode.ToString(); } }

        public string ShippingCity { get { return _innerOrder.ShipAddress.City; } }

        public string ShippingPhone { get { return _innerOrder.ShipAddress.Phone; } }

        private string _totalPrice;
        public string TotalPrice
        {
            get 
            {
                return _totalPrice;
            }
            set
            {
                _totalPrice = value;
                RaisePropertyChanged(() => TotalPrice);
            }
        }

        private string _orderDate;
        public string OrderDate
        {
            get { return _orderDate; }
            set 
            { 
                _orderDate = value;
                RaisePropertyChanged(() => OrderDate);
            }
        }

        private string modificationDate;
        public string ModificationDate
        {
            get { return modificationDate; }
            set
            {
                if (modificationDate != value)
                {
                    modificationDate = value;
                    RaisePropertyChanged(() => this.ModificationDate);
                }
            }
        }

        private bool isShipped;
        public bool IsShipped
        {
            get { return isShipped; }
            set
            {
                if (isShipped != value)
                {
                    isShipped = value;
                    RaisePropertyChanged(() => this.IsShipped);
                }
            }
        }

        

        private string _state;
        public string State
        {
            get { return _state; }
            set
            {
                _state = value;
                RaisePropertyChanged(() => State);
            }
        }

        public int Id
        {
            get { return _innerOrder.Id; }
        }

        private readonly ObservableCollection<ProductViewModel> _products = new ObservableCollection<ProductViewModel>();
        public ObservableCollection<ProductViewModel> Products
        {
            get { return _products; }
        } 

    }
}
